home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elispman.lha / elispman / elisp-24 (.txt) < prev    next >
GNU Info File  |  1993-06-01  |  48KB  |  839 lines

  1. This is Info file elisp, produced by Makeinfo-1.55 from the input file
  2. elisp.texi.
  3.    This is edition 2.0 of the GNU Emacs Lisp Reference Manual, for
  4. Emacs Version 19.
  5.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  6. Cambridge, MA 02139 USA
  7.    Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.    Permission is granted to copy and distribute modified versions of
  12. this manual under the conditions for verbatim copying, provided that
  13. the entire resulting derived work is distributed under the terms of a
  14. permission notice identical to this one.
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions, except that this permission notice may be stated in a
  18. translation approved by the Foundation.
  19. File: elisp,  Node: Case Changes,  Next: Text Properties,  Prev: Columns,  Up: Text
  20. Case Changes
  21. ============
  22.    The case change commands described here work on text in the current
  23. buffer.  *Note Character Case::, for case conversion commands that work
  24. on strings and characters.  *Note Case Table::, for how to customize
  25. which characters are upper or lower case and how to convert them.
  26.  - Command: capitalize-region START END
  27.      This function capitalizes all words in the region defined by START
  28.      and END.  To capitalize means to convert each word's first
  29.      character to upper case and convert the rest of each word to lower
  30.      case.  The function returns `nil'.
  31.      If one end of the region is in the middle of a word, the part of
  32.      the word within the region is treated as an entire word.
  33.      When `capitalize-region' is called interactively, START and END
  34.      are point and the mark, with the smallest first.
  35.           ---------- Buffer: foo ----------
  36.           This is the contents of the 5th foo.
  37.           ---------- Buffer: foo ----------
  38.           
  39.           (capitalize-region 1 44)
  40.           => nil
  41.           
  42.           ---------- Buffer: foo ----------
  43.           This Is The Contents Of The 5th Foo.
  44.           ---------- Buffer: foo ----------
  45.  - Command: downcase-region START END
  46.      This function converts all of the letters in the region defined by
  47.      START and END to lower case.  The function returns `nil'.
  48.      When `downcase-region' is called interactively, START and END are
  49.      point and the mark, with the smallest first.
  50.  - Command: upcase-region START END
  51.      This function converts all of the letters in the region defined by
  52.      START and END to upper case.  The function returns `nil'.
  53.      When `upcase-region' is called interactively, START and END are
  54.      point and the mark, with the smallest first.
  55.  - Command: capitalize-word COUNT
  56.      This function capitalizes COUNT words after point, moving point
  57.      over as it does.  To capitalize means to convert each word's first
  58.      character to upper case and convert the rest of each word to lower
  59.      case.  If COUNT is negative, the function capitalizes the -COUNT
  60.      previous words but does not move point.  The value is `nil'.
  61.      If point is in the middle of a word, the part of word the before
  62.      point (if moving forward) or after point (if operating backward)
  63.      is ignored.  The rest is treated as an entire word.
  64.      When `capitalize-word' is called interactively, COUNT is set to
  65.      the numeric prefix argument.
  66.  - Command: downcase-word COUNT
  67.      This function converts the COUNT words after point to all lower
  68.      case, moving point over as it does.  If COUNT is negative, it
  69.      converts the -COUNT previous words but does not move point.  The
  70.      value is `nil'.
  71.      When `downcase-word' is called interactively, COUNT is set to the
  72.      numeric prefix argument.
  73.  - Command: upcase-word COUNT
  74.      This function converts the COUNT words after point to all upper
  75.      case, moving point over as it does.  If COUNT is negative, it
  76.      converts the -COUNT previous words but does not move point.  The
  77.      value is `nil'.
  78.      When `upcase-word' is called interactively, COUNT is set to the
  79.      numeric prefix argument.
  80. File: elisp,  Node: Text Properties,  Next: Substitution,  Prev: Case Changes,  Up: Text
  81. Text Properties
  82. ===============
  83.    Each character position in a buffer or a string can have a "text
  84. property list", much like the property list of a symbol.  The properties
  85. belong to a particular character at a particular place, such as, the
  86. letter `T' at the beginning of this sentence or the first `o' in
  87. `foo'--if the same character occurs in two different places, the two
  88. occurrences generally have different properties.
  89.    Each property has a name, which is usually a symbol, and an
  90. associated value, which can be any Lisp object--just as for properties
  91. of symbols (*note Property Lists::.).
  92.    If a character has a `category' property, we call it the "category"
  93. of the character.  It should be a symbol.  The properties of the symbol
  94. serve as defaults for the properties of the character.
  95.    Copying text between strings and buffers preserves the properties
  96. along with the characters; this includes such diverse functions as
  97. `substring', `insert', and `buffer-substring'.
  98. * Menu:
  99. * Examining Properties::    Looking at the properties of one character.
  100. * Changing Properties::        Setting the properties of a range of text.
  101. * Property Search::        Searching for where a property changes value.
  102. * Special Properties::        Particular properties with special meanings.
  103. * Not Intervals::        Why text properties do not use
  104.                   Lisp-visible text intervals.
  105. File: elisp,  Node: Examining Properties,  Next: Changing Properties,  Up: Text Properties
  106. Examining Text Properties
  107. -------------------------
  108.    The simplest way to examine text properties is to ask for the value
  109. of a particular property of a particular character.  For that, use
  110. `get-text-property'.  Use `text-properties-at' to get the entire
  111. property list of a character.  *Note Property Search::, for functions
  112. to examine the properties of a number of characters at once.
  113.    These functions handle both strings and buffers.  Keep in mind that
  114. positions in a string start from 0, whereas positions in a buffer start
  115. from 1.
  116.  - Function: get-text-property POS PROP &optional OBJECT
  117.      This function returns the value of the PROP property of the
  118.      character after position POS in OBJECT (a buffer or string).  The
  119.      argument OBJECT is optional and defaults to the current buffer.
  120.      If there is no PROP property strictly speaking, but the character
  121.      has a category which is a symbol, then `get-text-property' returns
  122.      the PROP property of that symbol.
  123.  - Function: text-properties-at POSITION &optional OBJECT
  124.      This function returns the list of properties held by the character
  125.      at POSITION in the string or buffer OBJECT.  If OBJECT is `nil',
  126.      it defaults to the current buffer.
  127. File: elisp,  Node: Changing Properties,  Next: Property Search,  Prev: Examining Properties,  Up: Text Properties
  128. Changing Text Properties
  129. ------------------------
  130.    The primitives for changing properties apply to a specified range of
  131. text.  The function `set-text-properties' (see end of section) sets the
  132. entire property list of the text in that range; more often, it is
  133. useful to add, change, or delete just certain properties specified by
  134. name.
  135.    Since text properties are considered part of the buffer's contents,
  136. and can affect how the buffer looks on the screen, any change in the
  137. text properties is considered a buffer modification.  Buffer text
  138. property changes are undoable.
  139.  - Function: add-text-properties START END PROPS &optional OBJECT
  140.      This function modifies the text properties for the text between
  141.      START and END in the string or buffer OBJECT.  If OBJECT is `nil',
  142.      it defaults to the current buffer.
  143.      The argument PROPS specifies which properties to change.  It
  144.      should have the form of a property list (*note Property Lists::.):
  145.      a list whose elements include the property names followed
  146.      alternately by the corresponding values.
  147.      The return value is `t' if the function actually changed some
  148.      property's value; `nil' otherwise (if PROPS is `nil' or its values
  149.      agree with those in the text).
  150.      For example, here is how to set the `comment' property to `t' for
  151.      a range of text:
  152.           (add-text-properties (region-beginning)
  153.                                (region-end)
  154.                                (list 'comment t))
  155.  - Function: put-text-property START END PROP VALUE &optional OBJECT
  156.      This function sets the PROP property to VALUE for the text between
  157.      START and END in the string or buffer OBJECT.  If OBJECT is `nil',
  158.      it defaults to the current buffer.
  159.  - Function: remove-text-properties START END PROPS &optional OBJECT
  160.      This function deletes specified text properties from the text
  161.      between START and END in the string or buffer OBJECT.  If OBJECT
  162.      is `nil', it defaults to the current buffer.
  163.      The argument PROPS specifies which properties to delete.  It
  164.      should have the form of a property list (*note Property Lists::.):
  165.      a list whose elements include the property names followed by the
  166.      corresponding values.  The property names mentioned in PROPS are
  167.      the ones deleted from the text.  The values associated in PROPS
  168.      with these names do not matter.
  169.      The return value is `t' if the function actually changed some
  170.      property's value; `nil' otherwise (if PROPS is `nil' or if none of
  171.      the text had any of those properties).
  172.  - Function: set-text-properties START END PROPS &optional OBJECT
  173.      This function completely replaces the text property list for the
  174.      text between START and END in the string or buffer OBJECT.  If
  175.      OBJECT is `nil', it defaults to the current buffer.
  176.      The argument PROPS is the new property list.  It should have the
  177.      form of a list whose elements include the property names followed
  178.      by the corresponding values.
  179.      After `set-text-properties' returns, all the characters in the
  180.      specified range have identical properties.
  181.      If PROPS is `nil', the effect is to get rid of all properties from
  182.      the specified range of text.  Here's an example:
  183.           (set-text-properties (region-beginning)
  184.                                (region-end)
  185.                                nil)
  186. File: elisp,  Node: Property Search,  Next: Special Properties,  Prev: Changing Properties,  Up: Text Properties
  187. Property Search Functions
  188. -------------------------
  189.    In typical use of text properties, most of the time several or many
  190. consecutive characters have the same value for a property.  Rather than
  191. writing your programs to examine characters one by one, it is much
  192. faster to process chunks of text that have the same property value.
  193.    Here are functions you can use to do this.  In all cases, OBJECT
  194. defaults to the current buffer.
  195.  - Function: next-property-change POS &optional OBJECT
  196.      The function scans the text forward from position POS in the
  197.      string or buffer OBJECT till it finds a change in some text
  198.      property, then returns the position of the change.  In other
  199.      words, it returns the position of the first character beyond POS
  200.      whose properties are not identical to those of the character just
  201.      after POS.
  202.      The value is `nil' if the properties remain unchanged all the way
  203.      to the end of OBJECT.  If the value is non-`nil', it is a position
  204.      greater than POS, never equal.
  205.      Here is an example of how to scan the buffer by chunks of text
  206.      within which all properties are constant:
  207.           (while (not (eobp))
  208.             (let ((plist (text-properties-at (point)))
  209.                   (next-change
  210.                    (or (next-property-change (point) (current-buffer))
  211.                        (point-max))))
  212.               PROCESS TEXT FROM POINT TO NEXT-CHANGE...
  213.               (goto-char next-change)))
  214.  - Function: next-single-property-change POS PROP &optional OBJECT
  215.      The function scans the text forward from position POS in the
  216.      string or buffer OBJECT till it finds a change in the PROP
  217.      property, then returns the position of the change.  In other
  218.      words, it returns the position of the first character beyond POS
  219.      whose PROP property differs from that of the character just after
  220.      POS.
  221.      The value is `nil' if the properties remain unchanged all the way
  222.      to the end of OBJECT.  If the value is non-`nil', it is a position
  223.      greater than POS, never equal.
  224.  - Function: previous-property-change POS &optional OBJECT
  225.      This is like `next-property-change', but scans back from POS
  226.      instead of forward.  If the value is non-`nil', it is a position
  227.      always strictly less than POS.
  228.  - Function: previous-single-property-change POS PROP &optional OBJECT
  229.      This is like `next-property-change', but scans back from POS
  230.      instead of forward.  If the value is non-`nil', it is a position
  231.      always strictly less than POS.
  232. File: elisp,  Node: Special Properties,  Next: Not Intervals,  Prev: Property Search,  Up: Text Properties
  233. Special Properties
  234. ------------------
  235.    If a character has a `category' property, we call it the "category"
  236. of the character.  It should be a symbol.  The properties of the symbol
  237. serve as defaults for the properties of the character.
  238.    You can use the property `face' to control the font and color of
  239. text.  *Note Faces::, for more information.  This feature is temporary;
  240. in the future, we may replace it with other ways of specifying how to
  241. display text.
  242.    The property `mouse-face' is used instead of `face' when the mouse
  243. is on or near the character.  For this purpose, "near" means that all
  244. text between the character and where the mouse is have the same
  245. `mouse-face' property value.
  246.    You can specify a different keymap for a portion of the text by means
  247. of a `local-map' property.  The property's value, for the character
  248. after point, replaces the buffer's local map.  *Note Active Keymaps::.
  249.    If a character has the property `read-only', then modifying that
  250. character is not allowed.  Any command that would do so gets an error.
  251.    If a character has the property `modification-hooks', then its value
  252. should be a list of functions; modifying that character calls all of
  253. those functions.  Each function receives two arguments: the beginning
  254. and end of the part of the buffer being modified.  Note that if a
  255. particular modification hook function appears on several characters
  256. being modified by a single primitive, you can't predict how many times
  257. the function will be called.
  258.    Insertion of text does not, strictly speaking, change any existing
  259. character, so there is a special rule for insertion.  It compares the
  260. `read-only' properties of the two surrounding characters; if they are
  261. non-`nil' and `eq' to each other, then the insertion is not allowed.
  262. Assuming insertion is allowed, it then gets the `modification-hooks'
  263. properties of those characters and calls all the functions in each of
  264. them.  (If a function appears on both characters, it may be called once
  265. or twice.)
  266.    See also *Note Change Hooks::, for other hooks that are called when
  267. you change text in a buffer.
  268.    The special properties `point-entered' and `point-left' record hook
  269. functions that report motion of point.  Each time point moves, Emacs
  270. compares these two property values:
  271.    * the `point-left' property of the character after the old location,
  272.      and
  273.    * the `point-entered' property of the character after the new
  274.      location.
  275. If these two values differ, each of them is called (if not `nil') with
  276. two arguments: the old value of point, and the new one.
  277.    The same comparison is made for the characters before the old and new
  278. locations.  The result may be to execute two `point-left' functions
  279. (which may be the same function) and/or two `point-entered' functions
  280. (which may be the same function).  The `point-left' functions are
  281. always called before the `point-entered' functions.
  282.    A primitive function may examine characters at various positions
  283. without moving point to those positions.  Only an actual change in the
  284. value of point runs these hook functions.
  285. File: elisp,  Node: Not Intervals,  Prev: Special Properties,  Up: Text Properties
  286. Why Text Properties are not Intervals
  287. -------------------------------------
  288.    Some editors that support adding attributes to text in the buffer do
  289. so by letting the user specify "intervals" within the text, and adding
  290. the properties to the intervals.  Those editors permit the user or the
  291. programmer to determine where individual intervals start and end.  We
  292. deliberately provided a different sort of interface in Emacs Lisp to
  293. avoid certain paradoxical behavior associated with text modification.
  294.    If the actual subdivision into intervals is meaningful, that means
  295. you can distinguish between a buffer that is just one interval with a
  296. certain property, and a buffer containing the same text subdivided into
  297. two intervals, both of which have that property.
  298.    Suppose you take the buffer with just one interval and kill part of
  299. the text.  The text remaining in the buffer is one interval, and the
  300. copy in the kill ring (and the undo list) becomes a separate interval.
  301. Then if you undo the kill, you get two intervals with the same
  302. properties.  Thus, the distinction can't be preserved when editing
  303. happens.
  304.    But suppose we "fix" this problem by coalescing the two intervals
  305. when the text is inserted.  That works fine if the buffer originally was
  306. a single interval.  But if it was two intervals, and the killed text
  307. equals one of them, then undoing the kill yields just one interval.
  308. Again, the distinction can't be preserved.
  309.    Insertion of text at the border between intervals also raises
  310. questions that have no satisfactory answer.
  311.    However, it is easy to arrange for editing to behave consistently for
  312. questions of the form, "What are the properties of this character?" So
  313. we have decided these are the only questions that make sense; we have
  314. not implemented asking questions about where intervals start or end.
  315.    For practical purposes, the property search functions serve in place
  316. of explicit interval boundaries.  You can think of them as finding the
  317. boundaries of intervals, assuming that intervals are always coalesced
  318. whenever possible.  *Note Property Search::.
  319.    Emacs also provides explicit intervals as a presentation feature; see
  320. *Note Overlays::.
  321. File: elisp,  Node: Substitution,  Next: Underlining,  Prev: Text Properties,  Up: Text
  322. Substituting for a Character Code
  323. =================================
  324.    The following functions replace characters within a specified region
  325. based on their character codes.
  326.  - Function: subst-char-in-region START END OLD-CHAR NEW-CHAR &optional
  327.           NOUNDO
  328.      This function replaces all occurrences of the character OLD-CHAR
  329.      with the character NEW-CHAR in the region of the current buffer
  330.      defined by START and END.
  331.      If NOUNDO is non-`nil', then `subst-char-in-region' does not
  332.      record the change for undo and does not mark the buffer as
  333.      modified.  This feature is useful for changes which are not
  334.      considered significant, such as when Outline mode changes visible
  335.      lines to invisible lines and vice versa.
  336.      `subst-char-in-region' does not move point and returns `nil'.
  337.           ---------- Buffer: foo ----------
  338.           This is the contents of the buffer before.
  339.           ---------- Buffer: foo ----------
  340.           
  341.           (subst-char-in-region 1 20 ?i ?X)
  342.                => nil
  343.           
  344.           ---------- Buffer: foo ----------
  345.           ThXs Xs the contents of the buffer before.
  346.           ---------- Buffer: foo ----------
  347.  - Function: translate-region START END TABLE
  348.      This function applies a translation table to the characters in the
  349.      buffer between positions START and END.
  350.      The translation table TABLE is a string; `(aref TABLE OCHAR)'
  351.      gives the translated character corresponding to OCHAR.  If the
  352.      length of TABLE is less than 256, any characters with codes larger
  353.      than the length of TABLE are not altered by the translation.
  354.      The return value of `translate-region' is the number of characters
  355.      which were actually changed by the translation.  This does not
  356.      count characters which were mapped into themselves in the
  357.      translation table.
  358.      This function is available in Emacs versions 19 and later.
  359. File: elisp,  Node: Underlining,  Next: Registers,  Prev: Substitution,  Up: Text
  360. Underlining
  361. ===========
  362.    The underlining commands are somewhat obsolete.  The
  363. `underline-region' function actually inserts `_^H' before each
  364. appropriate character in the region.  This command provides a minimal
  365. text formatting feature that might work on your printer; however, we
  366. recommend instead that you use more powerful text formatting facilities,
  367. such as Texinfo.
  368.  - Command: underline-region START END
  369.      This function underlines all nonblank characters in the region
  370.      defined by START and END.  That is, an underscore character and a
  371.      backspace character are inserted just before each non-whitespace
  372.      character in the region.  The backspace characters are intended to
  373.      cause overstriking, but in Emacs they display as either `\010' or
  374.      `^H', depending on the setting of `ctl-arrow'.  There is no way to
  375.      see the effect of the overstriking within Emacs.  The value is
  376.      `nil'.
  377.  - Command: ununderline-region START END
  378.      This function removes all underlining (overstruck underscores) in
  379.      the region defined by START and END.  The value is `nil'.
  380. File: elisp,  Node: Registers,  Next: Change Hooks,  Prev: Underlining,  Up: Text
  381. Registers
  382. =========
  383.    A register is a sort of variable used in Emacs editing that can hold
  384. a marker, a string, a rectangle, a window configuration (of one frame),
  385. or a frame configuration (of all frames).  Each register is named by a
  386. single character.  All characters, including control and meta characters
  387. (but with the exception of `C-g'), can be used to name registers.
  388. Thus, there are 255 possible registers.  A register is designated in
  389. Emacs Lisp by a character which is its name.
  390.    The functions in this section return unpredictable values unless
  391. otherwise stated.
  392.  - Variable: register-alist
  393.      This variable is an alist of elements of the form `(NAME .
  394.      cONTENTS)'.  Normally, there is one element for each Emacs
  395.      register that has been used.
  396.      The object NAME is a character (an integer) identifying the
  397.      register.  The object CONTENTS is a string, marker, or list
  398.      representing the register contents.  A string represents text
  399.      stored in the register.  A marker represents a position.  A list
  400.      represents a rectangle; its elements are strings, one per line of
  401.      the rectangle.
  402.  - Command: view-register REG
  403.      This command displays what is contained in register REG.
  404.  - Function: get-register REG
  405.      This function returns the contents of the register REG, or `nil'
  406.      if it has no contents.
  407.  - Function: set-register REG VALUE
  408.      This function sets the contents of register REG to VALUE.  A
  409.      register can be set to any value, but the other register functions
  410.      expect only certain data types.  The return value is VALUE.
  411.  - Command: point-to-register REG
  412.      This command stores both the current location of point and the
  413.      current buffer in register REG as a marker.
  414.  - Command: jump-to-register REG
  415.  - Command: register-to-point REG
  416.      This command restores the status recorded in register REG.
  417.      If REG contains a marker, it moves point to the position stored in
  418.      the marker.  Since both the buffer and the location within the
  419.      buffer are stored by the `point-to-register' function, this
  420.      command can switch you to another buffer.
  421.      If REG contains a window configuration or a frame configuration.
  422.      `jump-to-register' restores that configuration.
  423.  - Command: insert-register REG &optional BEFOREP
  424.      This command inserts contents of register REG into the current
  425.      buffer.
  426.      Normally, this command puts point before the inserted text, and the
  427.      mark after it.  However, if the optional second argument BEFOREP
  428.      is non-`nil', it puts the mark before and point after.  You can
  429.      pass a non-`nil' second argument BEFOREP to this function
  430.      interactively by supplying any prefix argument.
  431.      If the register contains a rectangle, then the rectangle is
  432.      inserted with its upper left corner at point.  This means that
  433.      text is inserted in the current line and underneath it on
  434.      successive lines.
  435.      If the register contains something other than saved text (a
  436.      string) or a rectangle (a list), currently useless things happen.
  437.      This may be changed in the future.
  438.  - Command: copy-to-register REG START END &optional DELETE-FLAG
  439.      This command copies the region from START to END into register
  440.      REG.  If DELETE-FLAG is non-`nil', it deletes the region from the
  441.      buffer after copying it into the register.
  442.  - Command: prepend-to-register REG START END &optional DELETE-FLAG
  443.      This command prepends the region from START to END into register
  444.      REG.  If DELETE-FLAG is non-`nil', it deletes the region from the
  445.      buffer after copying it to the register.
  446.  - Command: append-to-register REG START END &optional DELETE-FLAG
  447.      This command appends the region from START to END to the text
  448.      already in register REG.  If DELETE-FLAG is non-`nil', it deletes
  449.      the region from the buffer after copying it to the register.
  450.  - Command: copy-rectangle-to-register REG START END &optional
  451.           DELETE-FLAG
  452.      This command copies a rectangular region from START to END into
  453.      register REG.  If DELETE-FLAG is non-`nil', it deletes the region
  454.      from the buffer after copying it to the register.
  455.  - Command: window-configuration-to-register REG
  456.      This function stores the window configuration of the selected
  457.      frame in register REG.
  458.  - Command: frame-configuration-to-register REG
  459.      This function stores the current frame configuration in register
  460.      REG.
  461. File: elisp,  Node: Change Hooks,  Prev: Registers,  Up: Text
  462. Change Hooks
  463. ============
  464.    These hook variables let you arrange to take notice of all changes in
  465. all buffers (or in a particular buffer, if you make them buffer-local).
  466. See also *Note Special Properties::, for how to detect changes to
  467. specific parts of the text.
  468.  - Variable: before-change-function
  469.      If this variable is non-`nil', then it should be a function; the
  470.      function is called before any buffer modification.  Its arguments
  471.      are the beginning and end of the region that is going to change,
  472.      represented as integers.  The buffer that's about to change is
  473.      always the current buffer.
  474.  - Variable: after-change-function
  475.      If this variable is non-`nil', then it should be a function; the
  476.      function is called after any buffer modification.  It receives
  477.      three arguments: the beginning and end of the region just changed,
  478.      and the length of the text that existed before the change.  (To
  479.      get the current length, subtract the region beginning from the
  480.      region end.) All three arguments are integers.  The buffer that's
  481.      about to change is always the current buffer.
  482.    Both of these variables are temporarily bound to `nil' during the
  483. time that either of these hooks is running.  This means that if one of
  484. these functions changes the buffer, that change won't run these
  485. functions.  If you do want the hook function to be run recursively,
  486. write your hook functions to bind these variables back to their usual
  487. values.
  488.  - Variable: first-change-hook
  489.      This variable is a normal hook; its hook functions are run using
  490.      `run-hooks' whenever a buffer is changed that was previously in
  491.      the unmodified state.
  492.    The variables described in this section are meaningful only starting
  493. with Emacs version 19.
  494. File: elisp,  Node: Searching and Matching,  Next: Syntax Tables,  Prev: Text,  Up: Top
  495. Searching and Matching
  496. **********************
  497.    GNU Emacs provides two ways to search through a buffer for specified
  498. text: exact string searches and regular expression searches.  After a
  499. regular expression search, you can identify the text matched by parts of
  500. the regular expression by examining the "match data".
  501. * Menu:
  502. * String Search::         Search for an exact match.
  503. * Regular Expressions::   Describing classes of strings.
  504. * Regexp Search::         Searching for a match for a regexp.
  505. * Replacement::          Internals of `query-replace'.
  506. * Match Data::            Finding out which part of the text matched
  507.                             various parts of a regexp, after regexp search.
  508. * Standard Regexps::      Useful regexps for finding sentences, pages,...
  509. * Searching and Case::    Case-independent or case-significant searching.
  510. File: elisp,  Node: String Search,  Next: Regular Expressions,  Up: Searching and Matching
  511. Searching for Strings
  512. =====================
  513.    These are the primitive functions for searching through the text in a
  514. buffer.  They are meant for use in programs, but you may call them
  515. interactively.  If you do so, they prompt for the search string; LIMIT
  516. and NOERROR are set to `nil', and REPEAT is set to 1.
  517.  - Command: search-forward STRING &optional LIMIT NOERROR REPEAT
  518.      This function searches forward from point for an exact match for
  519.      STRING.  If successful, it sets point to the end of the occurrence
  520.      found, and returns the new value of point.  If no match is found,
  521.      the value and side effects depend on NOERROR (see below).
  522.      In the following example, point is positioned at the beginning of
  523.      the line.  Then `(search-forward "fox")' is evaluated in the
  524.      minibuffer and point is left after the last letter of `fox':
  525.           ---------- Buffer: foo ----------
  526.           -!-The quick brown fox jumped over the lazy dog.
  527.           ---------- Buffer: foo ----------
  528.           
  529.           (search-forward "fox")
  530.                => t
  531.           
  532.           ---------- Buffer: foo ----------
  533.           The quick brown fox-!- jumped over the lazy dog.
  534.           ---------- Buffer: foo ----------
  535.      The argument LIMIT specifies the upper bound to the search.  (It
  536.      must be a position in the current buffer.)  No match extending
  537.      after that position is accepted.  If LIMIT is omitted or `nil', it
  538.      defaults to the end of the accessible portion of the buffer.
  539.      What happens when the search fails depends on the value of
  540.      NOERROR.  If NOERROR is `nil', a `search-failed' error is
  541.      signaled.  If NOERROR is `t', `search-forward' returns `nil' and
  542.      does nothing.  If NOERROR is neither `nil' nor `t', then
  543.      `search-forward' moves point to the upper bound and returns `nil'.
  544.      (It would be more consistent now to return the new position of
  545.      point in that case, but some programs may depend on a value of
  546.      `nil'.)
  547.      If REPEAT is non-`nil', then the search is repeated that many
  548.      times.  Point is positioned at the end of the last match.
  549.  - Command: search-backward STRING &optional LIMIT NOERROR REPEAT
  550.      This function searches backward from point for STRING.  It is just
  551.      like `search-forward' except that it searches backwards and leaves
  552.      point at the beginning of the match.
  553.  - Command: word-search-forward STRING &optional LIMIT NOERROR REPEAT
  554.      This function searches forward from point for a "word" match for
  555.      STRING.  If it finds a match, it sets point to the end of the
  556.      match found, and returns the new value of point.
  557.      A word search differs from a simple string search in that a word
  558.      search *requires* that the words it searches for are present as
  559.      entire words (searching for the word `ball' does not match the word
  560.      `balls'), and punctuation and spacing are ignored (searching for
  561.      `ball boy' does match `ball.  Boy!').
  562.      In this example, point is first placed at the beginning of the
  563.      buffer; the search leaves it between the `y' and the `!'.
  564.           ---------- Buffer: foo ----------
  565.           -!-He said "Please!  Find
  566.           the ball boy!"
  567.           ---------- Buffer: foo ----------
  568.           
  569.           (word-search-forward "Please find the ball, boy.")
  570.                => t
  571.           
  572.           ---------- Buffer: foo ----------
  573.           He said "Please!  Find
  574.           the ball boy-!-!"
  575.           ---------- Buffer: foo ----------
  576.      If LIMIT is non-`nil' (it must be a position in the current
  577.      buffer), then it is the upper bound to the search.  The match
  578.      found must not extend after that position.
  579.      If NOERROR is `t', then `word-search-forward' returns `nil' when a
  580.      search fails, instead of signaling an error.  If NOERROR is
  581.      neither `nil' nor `t', then `word-search-forward' moves point to
  582.      LIMIT (or the end of the buffer) and returns `nil'.
  583.      If REPEAT is non-`nil', then the search is repeated that many
  584.      times.  Point is positioned at the end of the last match.
  585.  - Command: word-search-backward STRING &optional LIMIT NOERROR REPEAT
  586.      This function searches backward from point for a word match to
  587.      STRING.  This function is just like `word-search-forward' except
  588.      that it searches backward and normally leaves point at the
  589.      beginning of the match.
  590. File: elisp,  Node: Regular Expressions,  Next: Regexp Search,  Prev: String Search,  Up: Searching and Matching
  591. Regular Expressions
  592. ===================
  593.    A "regular expression" ("regexp", for short) is a pattern that
  594. denotes a (possibly infinite) set of strings.  Searching for matches for
  595. a regexp is a very powerful operation.  This section explains how to
  596. write regexps; the following section says how to search for them.
  597. * Menu:
  598. * Syntax of Regexps::       Rules for writing regular expressions.
  599. * Regexp Example::          Illustrates regular expression syntax.
  600. File: elisp,  Node: Syntax of Regexps,  Next: Regexp Example,  Up: Regular Expressions
  601. Syntax of Regular Expressions
  602. -----------------------------
  603.    Regular expressions have a syntax in which a few characters are
  604. special constructs and the rest are "ordinary".  An ordinary character
  605. is a simple regular expression which matches that character and nothing
  606. else.  The special characters are `$', `^', `.', `*', `+', `?', `[',
  607. `]' and `\'; no new special characters will be defined in the future.
  608. Any other character appearing in a regular expression is ordinary,
  609. unless a `\' precedes it.
  610.    For example, `f' is not a special character, so it is ordinary, and
  611. therefore `f' is a regular expression that matches the string `f' and
  612. no other string.  (It does *not* match the string `ff'.)  Likewise, `o'
  613. is a regular expression that matches only `o'.
  614.    Any two regular expressions A and B can be concatenated.  The result
  615. is a regular expression which matches a string if A matches some amount
  616. of the beginning of that string and B matches the rest of the string.
  617.    As a simple example, we can concatenate the regular expressions `f'
  618. and `o' to get the regular expression `fo', which matches only the
  619. string `fo'.  Still trivial.  To do something more powerful, you need
  620. to use one of the special characters.  Here is a list of them:
  621. `. (Period)'
  622.      is a special character that matches any single character except a
  623.      newline.  Using concatenation, we can make regular expressions
  624.      like `a.b' which matches any three-character string which begins
  625.      with `a' and ends with `b'.
  626.      is not a construct by itself; it is a suffix that means the
  627.      preceding regular expression is to be repeated as many times as
  628.      possible.  In `fo*', the `*' applies to the `o', so `fo*' matches
  629.      one `f' followed by any number of `o's.  The case of zero `o's is
  630.      allowed: `fo*' does match `f'.
  631.      `*' always applies to the *smallest* possible preceding
  632.      expression.  Thus, `fo*' has a repeating `o', not a repeating `fo'.
  633.      The matcher processes a `*' construct by matching, immediately, as
  634.      many repetitions as can be found.  Then it continues with the rest
  635.      of the pattern.  If that fails, backtracking occurs, discarding
  636.      some of the matches of the `*'-modified construct in case that
  637.      makes it possible to match the rest of the pattern.  For example,
  638.      matching `ca*ar' against the string `caaar', the `a*' first tries
  639.      to match all three `a's; but the rest of the pattern is `ar' and
  640.      there is only `r' left to match, so this try fails.  The next
  641.      alternative is for `a*' to match only two `a's.  With this choice,
  642.      the rest of the regexp matches successfully.
  643.      is a suffix character similar to `*' except that it must match the
  644.      preceding expression at least once.  So, for example, `ca+r' will
  645.      match the strings `car' and `caaaar' but not the string `cr',
  646.      whereas `ca*r' would match all three strings.
  647.      is a suffix character similar to `*' except that it can match the
  648.      preceding expression either once or not at all.  For example,
  649.      `ca?r' will match `car' or `cr'; nothing else.
  650. `[ ... ]'
  651.      `[' begins a "character set", which is terminated by a `]'.  In
  652.      the simplest case, the characters between the two form the set.
  653.      Thus, `[ad]' matches either one `a' or one `d', and `[ad]*'
  654.      matches any string composed of just `a's and `d's (including the
  655.      empty string), from which it follows that `c[ad]*r' matches `cr',
  656.      `car', `cdr', `caddaar', etc.
  657.      Character ranges can also be included in a character set, by
  658.      writing two characters with a `-' between them.  Thus, `[a-z]'
  659.      matches any lower case letter.  Ranges may be intermixed freely
  660.      with individual characters, as in `[a-z$%.]', which matches any
  661.      lower case letter or `$', `%' or a period.
  662.      Note that the usual special characters are not special any more
  663.      inside a character set.  A completely different set of special
  664.      characters exists inside character sets: `]', `-' and `^'.
  665.      To include a `]' in a character set, make it the first character.
  666.      For example, `[]a]' matches `]' or `a'.  To include a `-', write
  667.      `-' as the first or last character in the range.
  668.      To include `^', make it other than the first character in the set.
  669. `[^ ... ]'
  670.      `[^' begins a "complement character set", which matches any
  671.      character except the ones specified.  Thus, `[^a-z0-9A-Z]' matches
  672.      all characters *except* letters and digits.
  673.      `^' is not special in a character set unless it is the first
  674.      character.  The character following the `^' is treated as if it
  675.      were first (thus, `-' and `]' are not special there).
  676.      Note that a complement character set can match a newline, unless
  677.      newline is mentioned as one of the characters not to match.
  678.      is a special character that matches the empty string, but only at
  679.      the beginning of a line in the text being matched.  Otherwise it
  680.      fails to match anything.  Thus, `^foo' matches a `foo' which occurs
  681.      at the beginning of a line.
  682.      When matching a string, `^' matches at the beginning of the string
  683.      or after a newline character `\n'.
  684.      is similar to `^' but matches only at the end of a line.  Thus,
  685.      `x+$' matches a string of one `x' or more at the end of a line.
  686.      When matching a string, `$' matches at the end of the string or
  687.      before a newline character `\n'.
  688.      has two functions: it quotes the special characters (including
  689.      `\'), and it introduces additional special constructs.
  690.      Because `\' quotes special characters, `\$' is a regular
  691.      expression which matches only `$', and `\[' is a regular
  692.      expression which matches only `[', and so on.
  693.      Note that `\' also has special meaning in the read syntax of Lisp
  694.      strings (*note String Type::.), and must be quoted with `\'.  For
  695.      example, the regular expression that matches the `\' character is
  696.      `\\'.  To write a Lisp string that contains the characters `\\',
  697.      Lisp syntax requires you to quote each `\' with another `\'.
  698.      Therefore, the read syntax for a regular expression matching `\'
  699.      is `"\\\\"'.
  700.    *Please note:* for historical compatibility, special characters are
  701. treated as ordinary ones if they are in contexts where their special
  702. meanings make no sense.  For example, `*foo' treats `*' as ordinary
  703. since there is no preceding expression on which the `*' can act.  It is
  704. poor practice to depend on this behavior; better to quote the special
  705. character anyway, regardless of where it appears.
  706.    For the most part, `\' followed by any character matches only that
  707. character.  However, there are several exceptions: characters which,
  708. when preceded by `\', are special constructs.  Such characters are
  709. always ordinary when encountered on their own.  Here is a table of `\'
  710. constructs:
  711.      specifies an alternative.  Two regular expressions A and B with
  712.      `\|' in between form an expression that matches anything that
  713.      either A or B matches.
  714.      Thus, `foo\|bar' matches either `foo' or `bar' but no other string.
  715.      `\|' applies to the largest possible surrounding expressions.
  716.      Only a surrounding `\( ... \)' grouping can limit the grouping
  717.      power of `\|'.
  718.      Full backtracking capability exists to handle multiple uses of
  719.      `\|'.
  720. `\( ... \)'
  721.      is a grouping construct that serves three purposes:
  722.        1. To enclose a set of `\|' alternatives for other operations.
  723.           Thus, `\(foo\|bar\)x' matches either `foox' or `barx'.
  724.        2. To enclose a complicated expression for a suffix character
  725.           such as `*' to operate on.  Thus, `ba\(na\)*' matches
  726.           `bananana', etc., with any (zero or more) number of `na'
  727.           strings.
  728.        3. To record a matched substring for future reference.
  729.      This last application is not a consequence of the idea of a
  730.      parenthetical grouping; it is a separate feature which happens to
  731.      be assigned as a second meaning to the same `\( ... \)' construct
  732.      because there is no conflict in practice between the two meanings.
  733.      Here is an explanation of this feature:
  734. `\DIGIT'
  735.      matches the same text which is matched the DIGITth time by a
  736.      previous `\( ... \)' construct.
  737.      In other words, after the end of a `\( ... \)' construct.  the
  738.      matcher remembers the beginning and end of the text matched by
  739.      that construct.  Then, later on in the regular expression, you can
  740.      use `\' followed by DIGIT to mean "match the same text matched the
  741.      DIGITth time by the `\( ... \)' construct."
  742.      The strings matching the first nine `\( ... \)' constructs
  743.      appearing in a regular expression are assigned numbers 1 through 9
  744.      in the order that the open parentheses appear in the regular
  745.      expression.  So you can use `\1' through `\9' to refer to the text
  746.      matched by the corresponding `\( ... \)' constructs.
  747.      For example, `\(.*\)\1' matches any newline-free string that is
  748.      composed of two identical halves.  The `\(.*\)' matches the first
  749.      half, which may be anything, but the `\1' that follows must match
  750.      the same exact text.
  751.      matches the empty string, provided it is at the beginning of the
  752.      buffer.
  753.      matches the empty string, provided it is at the end of the buffer.
  754.      matches the empty string, provided it is at point.
  755.      matches the empty string, provided it is at the beginning or end
  756.      of a word.  Thus, `\bfoo\b' matches any occurrence of `foo' as a
  757.      separate word.  `\bballs?\b' matches `ball' or `balls' as a
  758.      separate word.
  759.      matches the empty string, provided it is *not* at the beginning or
  760.      end of a word.
  761.      matches the empty string, provided it is at the beginning of a
  762.      word.
  763.      matches the empty string, provided it is at the end of a word.
  764.      matches any word-constituent character.  The editor syntax table
  765.      determines which characters these are.  *Note Syntax Tables::.
  766.      matches any character that is not a word-constituent.
  767. `\sCODE'
  768.      matches any character whose syntax is CODE.  Here CODE is a
  769.      character which represents a syntax code: thus, `w' for word
  770.      constituent, `-' for whitespace, `(' for open parenthesis, etc.
  771.      *Note Syntax Tables::, for a list of the codes.
  772. `\SCODE'
  773.      matches any character whose syntax is not CODE.
  774.    Not every string is a valid regular expression.  For example, any
  775. string with unbalanced square brackets is invalid, and so is a string
  776. that ends with a single `\'.  If an invalid regular expression is
  777. passed to any of the search functions, an `invalid-regexp' error is
  778. signaled.
  779.  - Function: regexp-quote STRING
  780.      This function returns a regular expression string which matches
  781.      exactly STRING and nothing else.  This allows you to request an
  782.      exact string match when calling a function that wants a regular
  783.      expression.
  784.           (regexp-quote "^The cat$")
  785.                => "\\^The cat\\$"
  786.      One use of `regexp-quote' is to combine an exact string match with
  787.      context described as a regular expression.  For example, this
  788.      searches for the string which is the value of `string', surrounded
  789.      by whitespace:
  790.           (re-search-forward
  791.            (concat "\\s " (regexp-quote string) "\\s "))
  792. File: elisp,  Node: Regexp Example,  Prev: Syntax of Regexps,  Up: Regular Expressions
  793. Complex Regexp Example
  794. ----------------------
  795.    Here is a complicated regexp, used by Emacs to recognize the end of a
  796. sentence together with any whitespace that follows.  It is the value of
  797. the variable `sentence-end'.
  798.    First, we show the regexp as a string in Lisp syntax to enable you to
  799. distinguish the spaces from the tab characters.  The string constant
  800. begins and ends with a double-quote.  `\"' stands for a double-quote as
  801. part of the string, `\\' for a backslash as part of the string, `\t'
  802. for a tab and `\n' for a newline.
  803.      "[.?!][]\"')}]*\\($\\|\t\\|  \\)[ \t\n]*"
  804.    In contrast, if you evaluate the variable `sentence-end', you will
  805. see the following:
  806.      sentence-end
  807.      =>
  808.      "[.?!][]\"')}]*\\($\\|  \\|  \\)[
  809.      ]*"
  810. In this case, the tab and carriage return are the actual characters.
  811.    This regular expression contains four parts in succession and can be
  812. deciphered as follows:
  813. `[.?!]'
  814.      The first part of the pattern consists of three characters, a
  815.      period, a question mark and an exclamation mark, within square
  816.      brackets.  The match must begin with one of these three characters.
  817. `[]\"')}]*'
  818.      The second part of the pattern matches any closing braces and
  819.      quotation marks, zero or more of them, that may follow the period,
  820.      question mark or exclamation mark.  The `\"' is Lisp syntax for a
  821.      double-quote in a string.  The `*' at the end indicates that the
  822.      immediately preceding regular expression (a character set, in this
  823.      case) may be repeated zero or more times.
  824. `\\($\\|\t\\|  \\)'
  825.      The third part of the pattern matches the whitespace that follows
  826.      the end of a sentence: the end of a line, or a tab, or two spaces.
  827.      The double backslashes are needed to prevent Emacs from reading
  828.      the parentheses and vertical bars as part of the search pattern;
  829.      the parentheses are used to mark the group and the vertical bars
  830.      are used to indicated that the patterns to either side of them are
  831.      alternatives.  The dollar sign is used to match the end of a line.
  832.      The tab character is written using `\t' and the two spaces are
  833.      written as themselves.
  834. `[ \t\n]*'
  835.      Finally, the last part of the pattern indicates that the end of
  836.      the line or the whitespace following the period, question mark or
  837.      exclamation mark may, but need not, be followed by additional
  838.      whitespace.
  839.